package com.example.qyweixin.cases;

import com.example.qyweixin.QyweixinApplicationTests;
import com.example.qyweixin.config.RequestConfig;
import com.example.qyweixin.constants.Constants;
import com.example.qyweixin.dto.User;
import com.example.qyweixin.dto.RespMessage;
import com.example.qyweixin.service.UserService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class UserTestCase extends QyweixinApplicationTests {

    @Autowired
    RequestConfig config;

    @Autowired
    UserService service;


    /**
     * 提供通讯录管理功能的测试数据
     * @return
     */
    @DataProvider
    public Object[][] getUserData(){
        List<User> userData = service.getUserData();
        Object[][] objects = new Object[userData.size()][1];
        for (int i = 0; i < userData.size(); i++) {
            User user = userData.get(i);
            String[] str = user.getDepartments().split(",");
            int[] department = Arrays.asList(str).stream().mapToInt(Integer::parseInt).toArray();
            user.setDepartment(department);
            objects[i][0] = user;
            System.out.println(user);
        }

        return objects;
    }


    @Test(dataProvider = "getUserData")
    public void testCreateUser(User user) {
        //1、获取token
        String token = Constants.tokenMap.get("token");
        //2、获取创建对象的url
        String url = new StringBuilder(config.getBaseUrl()).append(config.getUserInfos().get("create-uri"))
                .append("?access_token=").append(token).append("&debug=1").toString();
        //3、创建请求对象
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost(url);
        //4、准备json请求参数
        ObjectMapper mapper = new ObjectMapper();
        try {
            String json = mapper.writeValueAsString(user);
            StringEntity params = new StringEntity(json, ContentType.create("application/json", "utf-8"));
            post.setEntity(params);
            System.out.println(params);
            //5、发送请求
            CloseableHttpResponse response = client.execute(post);
            //6、解析响应数据
            String respMessage = EntityUtils.toString(response.getEntity());
            RespMessage resp = mapper.readValue(respMessage, RespMessage.class);
            Assert.assertEquals(resp.getErrcode(),user.getExpectedCode());
            Assert.assertEquals(resp.getErrmsg(),user.getExpectedMsg());


        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }catch (IOException e){

        }catch (ParseException e){

        }
    }

    @Test
    public void testDeleteUser(){
        //1、获取token
        String token = Constants.tokenMap.get("token");
        //2、获取删除对象的url
        String url = new StringBuilder(config.getBaseUrl()).append(config.getUserInfos().get("delete-uri"))
                .append("?access_token=").append(token).append("&userid=").append(1).append("&debug=1").toString();
        //3、创建请求对象
        CloseableHttpClient client = HttpClients.createDefault();
        HttpGet get = new HttpGet(url);
        try{
            //4、发送请求
            CloseableHttpResponse response = client.execute(get);
            //5、解析响应
            String resp = EntityUtils.toString(response.getEntity());
            ObjectMapper mapper = new ObjectMapper();
            RespMessage respMessage = mapper.readValue(resp, RespMessage.class);
            Assert.assertEquals(respMessage.getErrcode().intValue(),0);
            Assert.assertEquals(respMessage.getErrmsg(),"deleted");
        }catch (IOException e){
            e.printStackTrace();
        }catch (ParseException e){

        }


    }

}
